package com.iweb.service;

import com.common.datasource.DataSourceUtil;
import com.common.model.DataGrid;
import com.common.model.Page;
import com.iweb.dao.TicketsDao;
import com.iweb.dao.TicketsDaolmpl;
import com.iweb.pojo.Ticket;

import java.util.List;

public class TicketService {


    public static Ticket findTicketById(String ticket_id){
        Ticket ticket_sum = null;
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        //创建数据库操作对象
        TicketsDao ticketsDao = new TicketsDaolmpl(dataSourceUtil.getConnection());
        try {
            //业务逻辑
            ticket_sum = ticketsDao.findTicketById(ticket_id);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            dataSourceUtil.close();
        }

        return ticket_sum;
    }


    public DataGrid<Ticket> findTicketsList(Page page){
        DataGrid<Ticket> dataGrid = new DataGrid<>();
        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        TicketsDao ticketsDao = new TicketsDaolmpl(dataSourceUtil.getConnection());

        try {

            long count =ticketsDao.selectCount();
            List<Ticket> list = ticketsDao.findAll(page);
            dataGrid.setCode(0);
            dataGrid.setMsg("");
            dataGrid.setCount(count);
            dataGrid.setData(list);

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            dataSourceUtil.close();
        }
        return dataGrid;
    }


    public void deleteById(String ticket_id) {
        // 链接数据库
        DataSourceUtil dataSourceUtil = new DataSourceUtil();
        dataSourceUtil.init();
        // 创建数据库操作对象
        TicketsDao ticketsDao = new TicketsDaolmpl(dataSourceUtil.getConnection());

        try {
            ticketsDao.deleteById(ticket_id);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            dataSourceUtil.close();
        }
    }

}
